﻿using System.Collections.Generic;
using System.Web.Mvc;
using Wikey.Domain.Components;
using Wikey.Entity;
using Wikey.Domain.Repository;
using Wikey.Domain.Web;
using Wikey.Utilities.Extensions;

namespace Wikey.Web.Mvc.Controllers {

    [HandleError]
    public class AccountController : Controller {

        private readonly IUserRepository UserRepository;

        public AccountController()
        {
            UserRepository = Factory.Get<IUserRepository>();
            Repo = Factory.Get<IRoleRepository>();

        }
        public ActionResult Roles() {
            ViewData.Model = Repo.SelectAll();
            return View();
        }
        private IRoleRepository Repo; 
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Role(FormCollection collection) {
            int? id = collection["id"].Int();
            Role foundRole;

            if (id.Value != 0 ) {
                foundRole = Repo.Find(id.Value);
            } else {
                foundRole = new Role();
            }

            foundRole.Name = collection["name"];
            int newId = Repo.Save(foundRole);
            var jsonResultData = new RoleSaveResult { Id = newId, NewRole = Repo.Find(newId), Success = true };

            JsonResult result = new JsonResult();
            result.Data = jsonResultData;
            result.ContentType = typeof(RoleSaveResult).FullName;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }
        private class RoleSaveResult {
            public bool Success { get; set; }
            public int Id { get; set; }
            public Role NewRole { get; set; }
        }
        public ActionResult Index(int? id )
        {
            if ( !id.HasValue )
            {
                ViewData.Model = UserRepository.SelectAll();
            }
            else
            {
                var foundUser = UserRepository.Find<User>(id.Value);

                ViewData.Model = new List<User> { foundUser };

            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Registration()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Registration( FormCollection collection)
        {
            var user =
                new User()
                    {
                        Name = collection["Name"],
                        Credential =
                            new Credentials
                                {
                                    Email = collection["email"],
                                    Username = collection["username"],
                                    Password = collection["password"]
                                }
                    };
            

            UserRepository.Save(user);

            return View("Complete");
        }
    
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Login()
        {
            if( Authorization.IsGuest )
                return View();
            else
            {
                return View();
            }
        }

        public RedirectResult LogOff()
        {
            Authorization.CurrentUser = null;
            return Redirect("~/");
        }
        public ActionResult Login( FormCollection collection )
        {
            var crd = 
                new Credentials
                    {
                        Username = collection["username"], 
                        Password = collection["password"]
                    };

            var user = UserRepository.FindByCredential(crd);
            
            if (user == null)
            {
                ViewData["Message"] = "Ooppss ! Login failed...";
                return View() ;
            } 
           
            Session["user"] = user;
            return Redirect("~/My/" + user.Credential.Username);

        }

        public new ActionResult User(string username)
        {
           
            var user = username.Int() > 0 ? UserRepository.Find<User>(username.Int()) : UserRepository.FindByName(username);
            if (user == null)
                return View();

            user.Pages = UserRepository.FindPages(user);

            ViewData.Model = user;
            return View();
        }

        public ActionResult Edit( int? id )
        {
            ViewData.Model = UserRepository.Find<User>(id.Value);
            return View();
        }

      
    }  
}
